package com.wang.concurrency;

public class Account2 {

	private double balance = 0.0;
	
	private boolean haveBalance = false;

	public double getBalance() {
		return balance;
	}
	
	
/**
 *取钱方法 
 * @param amount
 * 取钱的数量
 * @throws InterruptedException 
 */
	public synchronized void draw(double amount) throws InterruptedException{
		while(!haveBalance){
			//wait()方法,会释放同步监视器
			wait();
		}
		System.out.printf("%s取钱:",Thread.currentThread().getName());
		balance -= amount;
		System.out.printf(",当前余额 %f%n ",balance);
		haveBalance = false;
		notifyAll();		
	}
	
	/**
	 * 存钱方法
	 * @param amount
	 * @throws InterruptedException
	 */
	public synchronized void deposit(double amount) throws InterruptedException{
		while(haveBalance){
			wait();
		}
		System.out.printf("%s存钱:",Thread.currentThread().getName());
		balance += amount;
		System.out.printf(",当前余额%f%n",balance);
		haveBalance = true;
		notifyAll();
			
	}	
}
